﻿@inject IDialogService DialogService
@inject ISearchService searchService
@inject IUserStatusService statusService
@inject IUserConfigService configService
@inject NavigationManager NavigationManager
@inject ILogger<SearchBar> logger

@implements IDisposable

<div class="searchbar">
    <div class="searchfilter">
        <ConflatedTextBox IconClass="fa-search" id="searchBox" placeholder="Enter a search term" OnValueChanged="@DoSearch" TextValue="@searchService.SearchText"/>
    </div>
    <div class="searchhint">
        <button class="btn btn-primary damselfly-searchtools" title="Refresh Search" @onclick="@(() => searchService.Refresh())">
            <i class="fas fa-redo-alt"/>
        </button>
        <button class="btn btn-primary damselfly-searchtools" title="Reset Search" @onclick="@(() => searchService.Reset())">
            <i class="fas fa-times"/>
        </button>
        <button class="btn btn-primary damselfly-searchtools" title="Advanced Search" @onclick="ToggleAdvancedSearch">
            @if( ShowAdvancedSearch )
            {
                <i class="fas fa-chevron-up"/>
            }
            else
            {
                <i class="fas fa-chevron-down"/>
            }
        </button>
        <button class="btn btn-primary damselfly-searchtools" title="People Management" @onclick="@PeopleManagement">
            <i class="fas fa-users"/>
        </button>
        <button class="btn btn-primary damselfly-searchtools" title="Tag Management" @onclick="@TagManagement">
            <i class="fas fa-tags"/>
        </button>
        @if( searchService.SearchHints.Any() )
        {
            <div class="search-hints">
                <Bricks Items="@searchService.SearchHints" ToStrFunc="x => x.Description"
                        RemoveLabel="Clear filter" ItemRemoved="x => x.Clear()"/>
            </div>
        }
        else
        {
            <label class="damselfly-browsetoollabel">No filter.</label>
        }

    </div>

    <div class="damselfly-browsetools">
        <select class="damselfly-browsetool damselfly-browsetool-select" @bind="SelectedGrouping">
            <option value="@GroupingType.None">Timeline</option>
            <option value="@GroupingType.Date">Date</option>
            <option value="@GroupingType.Folder">Folder</option>
        </select>
        <div class="damselfly-browsetool @SelectedStyle(GridImageSize.Small)">
            <i title="Small Thumbnails" class="fas fa-image" style="font-size:smaller;" @onclick="() => ChangeGridSize(GridImageSize.Small)"/>
        </div>
        <div class="damselfly-browsetool @SelectedStyle(GridImageSize.Medium)">
            <i title="Medium Thumbnails" class="fas fa-image" @onclick="() => ChangeGridSize(GridImageSize.Medium)"/>
        </div>
        <div class="damselfly-browsetool @SelectedStyle(GridImageSize.Large)">
            <i title="Large Thumbnails" class="fas fa-image" style="font-size:larger;" @onclick="() => ChangeGridSize(GridImageSize.Large)"/>
        </div>
        @if( searchService.SortOrder == SortOrderType.Descending )
        {
            <div class="damselfly-browsetool">
                <i title="Sort Ascending" class="fas fa-sort-amount-up" @onclick="() => ChangeSortOrder(SortOrderType.Ascending)"/>
            </div>
        }
        else
        {
            <div class="damselfly-browsetool">
                <i title="Sort Descending" class="fas fa-sort-amount-down" @onclick="() => ChangeSortOrder(SortOrderType.Descending)"/>
            </div>
        }
    </div>
    @if( ShowAdvancedSearch )
    {
        <AdvancedSearchPanel/>
    }
</div>

@code {

    [Parameter] public Action<GridImageSize> OnGridSizeChanged { get; set; }

    GridImageSize currentGridSize = GridImageSize.Small;
    bool ShowAdvancedSearch { get; set; } = false;

    private GroupingType SelectedGrouping
    {
        get => searchService.Grouping;
        set => ChangeGroupType(value);
    }

    private void ChangeGridSize(GridImageSize newSize)
    {
        currentGridSize = newSize;
        OnGridSizeChanged?.Invoke(newSize);
        StateHasChanged();
    }

    private string SelectedStyle(GridImageSize size)
    {
        return currentGridSize != size ? "damselfly-browsetool-deselected" : string.Empty;
    }

    protected override void OnInitialized()
    {
        currentGridSize = configService.Get(ConfigSettings.GridImageSize, GridImageSize.Small);
        OnGridSizeChanged?.Invoke(currentGridSize);
    }

    private void ToggleAdvancedSearch()
    {
        ShowAdvancedSearch = !ShowAdvancedSearch;
        StateHasChanged();
    }

    private void PeopleManagement()
    {
        try
        {
            NavigationManager.NavigateTo("/people");
        }
        catch( Exception ex )
        {
            logger.LogError("Exception: " + ex.Message);
        }
    }

    private void TagManagement()
    {
        try
        {
            NavigationManager.NavigateTo("/tags");
        }
        catch( Exception ex )
        {
            logger.LogError("Exception: " + ex.Message);
        }
    }

    protected override void OnAfterRender(bool firstRender)
    {
        if( firstRender )
        {
            searchService.OnSearchQueryChanged += SearchQueryChanged;
        }
    }

    public void Dispose()
    {
        searchService.OnSearchQueryChanged -= SearchQueryChanged;
    }

    protected void SearchQueryChanged()
    {
        StateHasChanged();
    }

    private void DoSearch(string searchTerm)
    {
        searchService.SearchText = searchTerm;
    }

    private void ChangeGroupType(GroupingType newType)
    {
        searchService.Grouping = newType;
        logger.LogInformation($"Grouping changed to {searchService.Grouping}");
        StateHasChanged();
    }

    private void ChangeSortOrder(SortOrderType newType)
    {
        searchService.SortOrder = newType;
        logger.LogInformation($"Sort order changed to {searchService.SortOrder}");
        StateHasChanged();
    }

}